//
//  HelpListViewController.m
//  ForYourBeauty
//
//  Created by Kelly xu on 2019/8/3.
//  Copyright © 2019 changrenTech. All rights reserved.
//

#import "HelpListViewController.h"
#import "FinderViewModel.h"
#import "HelpListCell.h"
#import "FinderDetailViewController.h"
@interface HelpListViewController ()<UITableViewDelegate,UITableViewDataSource>{
    
}
@property(nonatomic,weak)IBOutlet UITableView *mainTableView;
@property(nonatomic,strong)FinderViewModel *finderViewModel;
@property(nonatomic,strong)NSMutableArray *finderList;
@property(nonatomic,assign)BOOL isRequest;
@end

@implementation HelpListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationController.navigationBarHidden = NO;
    self.navigationItem.title = @"帮助";
    [self configMainTableView];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [self.finderViewModel fetchFinderList:@"WTBZ" keyWord:@"" cmp:^(NSMutableArray * _Nonnull finderList) {
        [self.finderList removeAllObjects];
        [self.finderList addObjectsFromArray:finderList];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.mainTableView reloadData];
        });
    }];
}
#pragma mark - UITableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.finderList count];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FinderListModel *model = [self.finderList objectAtIndex:indexPath.row];
    HelpListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HelpListCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    cell.titleLab.text = [NSString stringWithFormat:@"%@",model.articleTitle];
    if (indexPath.row==0) {
        [cell.bg setImage:[[UIImage imageNamed:@"topBg"] stretchableImageWithLeftCapWidth:[UIImage imageNamed:@"topBg"].size.width/2 topCapHeight:[UIImage imageNamed:@"topBg"].size.height/2]];
    }else if (indexPath.row<[self.finderList count]-1){
        [cell.bg setImage:[[UIImage imageNamed:@"middleBg"] stretchableImageWithLeftCapWidth:[UIImage imageNamed:@"middleBg"].size.width/2 topCapHeight:[UIImage imageNamed:@"middleBg"].size.height/2]];
    }else{
        [cell.bg setImage:[[UIImage imageNamed:@"bottomBg"] stretchableImageWithLeftCapWidth:[UIImage imageNamed:@"bottomBg"].size.width/2 topCapHeight:[UIImage imageNamed:@"bottomBg"].size.height/2]];
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    FinderDetailViewController *detail = [[FinderDetailViewController alloc]init];
    FinderListModel *model = [self.finderList objectAtIndex:indexPath.row];
    detail.finderId = [NSString stringWithFormat:@"%.0f",model.internalBaseClassIdentifier];
    [self.rt_navigationController pushViewController:detail animated:YES complete:nil];
}
#pragma mark - instance
-(void)configMainTableView{
    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    self.mainTableView.tableFooterView = [UIView new];
    self.mainTableView.backgroundColor = [UIColor clearColor];
    [self.mainTableView registerNib:[UINib nibWithNibName:@"HelpListCell" bundle:nil] forCellReuseIdentifier:@"HelpListCell"];
    self.mainTableView.showsVerticalScrollIndicator = NO;
}
-(FinderViewModel*)finderViewModel{
    if (!_finderViewModel) {
        _finderViewModel = [[FinderViewModel alloc]init];
    }
    return _finderViewModel;
}
-(NSMutableArray*)finderList{
    if (!_finderList) {
        _finderList = [[NSMutableArray alloc]init];
    }
    return _finderList;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
